package org.sja.log2traffic;

import org.sja.log2traffic.outputers.GeneralStatisticToJSON;
import org.sja.log2traffic.outputers.MultipleAxisChartRenderer;

public class LogAnalyzerFactory {

	private static LogAnalyzerFactory instance;
	private DataCalculator dataCalculator;
	private ConfigurationManipulator configurationManipulator;
	private GeneralStatisticToJSON generalStatisticToJSON;
	private MultipleAxisChartRenderer multipleAxisChartRenderer;
	private LogAnalyzerMain logAnalyzerMain;
	private LogAnalyzer logAnalyzer;
	private StatusOutputerImpl statusOutputerImpl;
	private FileNameProviderImpl fileNameProviderImpl;
	private FileOperations fileOperationsImpl;

	public static LogAnalyzerFactory getInstance() {
		if (instance == null) {
			instance = new LogAnalyzerFactory();
		}
		return instance;
	}

	public ILogLineProvider provideLogLineProvider() {
		return new LogLineProvider();
	}

	private DataCalculator provideDataCalculator() {
		if (dataCalculator == null) {
			dataCalculator = new DataCalculator();
		}
		return dataCalculator;
	}

	public IDateTimeFormatter provideDateTimeFormatter() {
		return provideDataCalculator();
	}

	public ITimeAgregatorCalculator provideTimeAgregatorCalculator() {
		return provideDataCalculator();
	}

	public ISimpleTimeCalculator provideSimpleTimeCalculator() {
		return provideDataCalculator();
	}

	public LogAnalyzer provideNewLogAnalyzer() {
		if (logAnalyzer == null) {
			logAnalyzer = new LogAnalyzer();
		}
		return logAnalyzer;
	}

	public ConfigurationManipulator provideConfigurationManipulator() {
		if (configurationManipulator == null) {
			configurationManipulator = new ConfigurationManipulator(provideDataCalculator());
		}
		return configurationManipulator;
	}

	public GeneralStatisticToJSON provideGeneralStatisticToJSON() {
		if (generalStatisticToJSON == null) {
			generalStatisticToJSON = new GeneralStatisticToJSON(provideDateTimeFormatter(),provideFileNameProvider());
		}
		return generalStatisticToJSON;
	}

	public MultipleAxisChartRenderer provideMultipleAxisChartRenderer() {
		if (multipleAxisChartRenderer == null) {
			multipleAxisChartRenderer = new MultipleAxisChartRenderer(provideDateTimeFormatter(),provideFileNameProvider(),provideStatisticCalculatorImpl());
		}
		return multipleAxisChartRenderer;
	}

	public LogAnalyzerMain provideLogAnalyzerMain() {
		if (logAnalyzerMain == null) {
			logAnalyzerMain = new LogAnalyzerMain(provideConfigurationManipulator(), provideStatusOutputer(), provideNewLogAnalyzer(), provideMultipleAxisChartRenderer(), provideGeneralStatisticToJSON(), provideTimeAgregatorCalculator(), provideSimpleTimeCalculator(), provideFileOperations());
		}
		return logAnalyzerMain;
	}

	public IStatusOutputer provideStatusOutputer() {
		if (statusOutputerImpl == null) {
			statusOutputerImpl = new StatusOutputerImpl();
		}
		return statusOutputerImpl;
	}

	private FileNameProviderImpl provideFileNameProviderImpl() {
		if (fileNameProviderImpl == null) {
			fileNameProviderImpl = new FileNameProviderImpl();
		}
		return fileNameProviderImpl;
	}

	public IStatisticCalculator provideStatisticCalculatorImpl() {
		return provideDataCalculator();
	}	
	
	public IFileNameProvider provideFileNameProvider() {
		return provideFileNameProviderImpl();
	}
	
	public IFileOperations provideFileOperations() {
		if (fileOperationsImpl == null) {
			fileOperationsImpl = new FileOperations();
		}
		return fileOperationsImpl;
	}
	
}
